<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>用户管理</title>
    <link rel="stylesheet" th:href="@{/layui/css/layui.css}" media="all" />
</head>
<body>
<!--模糊查询部分-->
<form class="layui-form" id="searchForm" lay-filter="searchForm">
    <blockquote class="layui-elem-quote quoteBox">
        <form class="layui-form">
            <span>用户ID:</span>&nbsp;&nbsp;
            <div class="layui-inline">
                <input class="layui-input" name="id" id="id" placeholder="用户ID" autocomplete="off">
            </div>
            <span>用户名:</span>&nbsp;&nbsp;
            <div class="layui-inline">
                <input class="layui-input" name="name" id="name" placeholder="用户名称" autocomplete="off">
            </div>
            <span>用户账号:</span>&nbsp;&nbsp;
            <div class="layui-inline">
                <input class="layui-input" name="accountId" id="location'" placeholder="账号" autocomplete="off">
            </div>
            <span>用户星级:</span>&nbsp;&nbsp;
                <div class="layui-inline">
                    <select name="rank" lay-filter="aihao">
                        <option value="" selected="">全部等级</option>
                        <option value="0">青铜</option>
                        <option value="1">白银</option>
                        <option value="2">黄金</option>
                        <option value="3">钻石</option>
                        <option value="4">星耀</option>
                        <option value="5">王者</option>
                        <option value="6">最强王者</option>
                    </select>
                </div>

            <div class="layui-inline">
                &nbsp;&nbsp;
                <button type="button" class="layui-btn layui-btn-normal"  lay-submit="" lay-filter="doSearch"><span class="layui-icon layui-icon-search"></span>搜索</button></legend>
                <button type="reset" class="layui-btn layui-btn-warm" ><span class="layui-icon layui-icon-refresh"></span>重置</button></legend>
            </div>
        </form>
    </blockquote>
</form>

<!--设置星级用户弹出框-->
<div style="display: none;padding: 10px" id="RankModal">
    <form action="" method="post" class="layui-form " id="dataFrm" lay-filter="dataFrm">
        <div class="layui-form-item">
            <input type="hidden" name="id">
            <label class="layui-form-label">星级</label>
            <div class="layui-input-block">
                <input type="radio" name="rank" value="0" title="青铜" checked="">
                <input type="radio" name="rank" value="1" title="白银">
                <input type="radio" name="rank" value="2" title="黄金">
                <input type="radio" name="rank" value="3" title="钻石">
                <input type="radio" name="rank" value="4" title="星耀">
                <input type="radio" name="rank" value="5" title="王者">
                <input type="radio" name="rank" value="6" title="最强王者" >

            </div>
        </div>
        <div class="layui-form-item layui-row layui-col-xs12" >
            <div class="layui-input-block">
                <button type="button" class="layui-btn layui-btn-normal" lay-submit="" lay-filter="doSubmit">赋予星级</button>
                <button type="reset" class="layui-btn layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
</div>

<!--用户管理表格-->
<!--数据表格-->
<table class="layui-hide" id="usersTable" lay-filter="usersTable"></table>
<!--表格工具条-->
<!--<div id="toolBar" style="display: none;">-->
    <!--<button type="button" class="layui-btn  layui-bg-blue layui-btn-sm"  lay-event="add"><span class="layui-icon">&#xe608;</span>添加用户</button>-->
    <!--<button type="button" class="layui-btn  layui-btn-sm layui-btn-danger"  lay-event="batchDelete" ><i class="layui-icon"></i>批量删除</button>-->
    <!--<button type="button" class="layui-btn  layui-btn-sm layui-btn-warm"  lay-event="reSet" ><span class="layui-icon ">&#xe669;</span>重置用户</button>-->
<!--</div>-->
<!--单行工具条-->
<script type="text/html" id="lineBar">
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="delete">删除用户</a>
    <a class="layui-btn layui-btn-xs" lay-event="openRankModal">设置星级</a>
</script>


<script th:src="@{/layui/layui.js}"></script>
</body>
</html>
<script  type="text/javascript">
    layui.use(['laydate', 'laypage', 'layer', 'table','form'], function(){
        var laydate = layui.laydate //日期
        var table = layui.table //表格
        var form=layui.form;
        var $=layui.jquery;

        //加载数据
        var tableIns= table.render({
            elem: '#usersTable'
            ,url:'/admin/userManager/list'
            ,title: '用户表'
            ,page: true //开启分页
            ,height:'full-75'
            ,toolbar: '#toolBar' //开启工具栏，此处显示默认图标，可以自定义模板，详见文档
            ,cols: [ [
                {type: 'checkbox', fixed: 'left'}
                ,{field: 'id', title: 'ID', width:80, sort: true, fixed: 'left', totalRowText: '合计：'}
                ,{field: 'accountId', title: '账号', width:80, sort: true}
                ,{field: 'rank', title: '星级', width:120, sort: true,templet:function (d) {
                        var rank="";
                        switch (d.rank) {
                            case 0:rank="<font style='color: grey'>青铜(<span class='layui-icon'>&#xe60c;</span>)</font>"
                                break;
                            case 1:rank="<font style='color: burlywood'>白银(<span class='layui-icon'>&#xe653;</span>)</font>"
                                break;
                            case 2:rank="<font style='color: #ffad13'>黄金(<span class='layui-icon'>&#xe659;</span>)</font>"
                                break;
                            case 3:rank="<font style='color: #303030'>钻石(<span class='layui-icon'>&#xe735;</span>)</font>"
                                break;
                            case 4:rank="<font style='color: green'>星耀(<span class='layui-icon'>&#xe636;</span>)</font>"
                                break;
                            case 5:rank="<font style='color: darkorchid'>王者(<span class='layui-icon'>&#xe67a;</span>)</font>"
                                break;
                            case 6:rank="<font style='color: darkorange'>最强王者(<span class='layui-icon'>&#xe67a;</span>)</font>"
                                break;
                        }
                        return rank;
                    }}
                ,{field: 'browser',width:80, title: '头像',templet: '<div><img style="height:20px;width:20px;border-radius:50%;line-height:50px!important;" src="{{ d.avatarUrl}}"></div>'}
                ,{field: 'name', title: '用户名'}
                ,{field: 'company', title: '公司', sort: true}
                ,{field: 'bio', title: '简介', sort: true}
                ,{field: 'location', title: '地点', sort: true, totalRow: true}
                ,{fixed: 'right',title:'操作' , align:'center', toolbar: '#lineBar'}
            ] ]
        });
        //模糊查询
        form.on('submit(doSearch)',function (data) {
            tableIns.reload({
                where:data.field,
                page:{
                    curr:1
                }
            });
        })
        //监听工具条的事件
        table.on("toolbar(usersTable)",function(obj){
            var data = obj.data;
            switch(obj.event){
                case 'batchDelete':
                    batchDelete(data);
                    break;
            };
        });
        //监听行工具条的事件
        table.on("tool(usersTable)",function(obj){
            var data = obj.data; //获得当前行数据
            switch(obj.event){
                case 'delete':
                    deleteUser(data);
                    break;
                case 'openRankModal':
                    openRankModal(data);
                    break;
            };
        });
        //删除
        function deleteUser(data) {
            layer.confirm('你确定要删除['+data.name+']这个用户吗?', {icon: 3, title:'提示'}, function(index){
                $.get("/admin/userManager/delete",{id:data.id},function(res){
                    if(res.code==1000){
                        tableIns.reload();
                        layer.msg("删除成功");
                        return;
                    }
                    layer.msg(res.msg);
                })
                layer.close(index);
            });
        }

        var mainIndex;
        //打开添加的弹出层
        var url;
        function openRankModal(data){
            mainIndex=layer.open({
                type:1,
                content: $("#RankModal"),
                area:['800px','200px'],
                title:'设置星级用户',
                success:function(){
                    form.val("dataFrm",data);
                    url="/admin/userManager/setRank";
                }
            });
        }

        //更新
        form.on('submit(doSubmit)',function (data) {
            $.post(url,data.field,function(res){
                if(res.code==1000){
                    layer.msg("更新等级成功");
                    tableIns.reload();
                }
                layer.close(mainIndex);
            })
            return false;
        })

        //批量删除
        function batchDelete() {
            //得到选中行
            var checkStatus = table.checkStatus('loginLogTable');
            var dataLength=checkStatus.data.length;
            if(dataLength>0){
                layer.confirm('你确定要删除这些数据吗?', {icon: 3, title:'提示'}, function(index){
                    var data=checkStatus.data; //获取选中行的数据
                    var ids="";
                    $.each(data,function(index,item){
                        ids+=item.id+",";
                    })
                    ids=ids.substring(0,ids.length-1);
                    $.get("/login/batchDelete",{"ids":ids},function(res){
                        if(res.code==0){
                            tableIns.reload();
                        }
                        layer.msg(res.msg);
                    })
                    layer.close(index);
                });

            }else{
                layer.msg("请选中操作行")
            }
        }
    });
</script>